/**
 * Created by zhangsanfeng on 2020/11/11.
 */

import {Message} from 'element-ui';

export const tool = {
    formatDateTime(a) {
        let c = a < 11111111111 ? a * 1000 : a;
        let t = new Date(c);
        let d = {
            y: t.getFullYear(),
            m: this.ddigit(t.getMonth() + 1),
            d: this.ddigit(t.getDate()),
            h: this.ddigit(t.getHours()),
            i: this.ddigit(t.getMinutes()),
            s: this.ddigit(t.getSeconds())
        }
        return {
            dateTime: !!a ? (d.y + '-' + d.m + '-' + d.d + ' ' + d.h + ':' + d.i + ':' + d.s) : '-',
            date: !!a ? (d.y + '-' + d.m + '-' + d.d) : '-',
            array: d
        }
    },

    ddigit: function (a) {
        return a < 10 ? '0' + a : a;
    },

    mathRandom: function (min, max) {
        return Math.floor(Math.random() * (max - min + 1) + min);
    },

    //success warning error
    message(text, type, callback, duration) {
        Message({
            message: text,
            type: !!type ? type : 'success',
            duration: !!duration ? duration : 1800,
            onClose: () => {
                callback && callback();
            }
        })
    },

    setCookie: function (name, value, iDay, iPath) {
        let oDate = new Date();
        iDay = arguments[2] ? arguments[2] : 7;
        iPath = arguments[3] ? arguments[3] : "/";
        oDate.setDate(oDate.getDate() + iDay);
        document.cookie = name + "=" + encodeURI(value) + ";expires=" + oDate + ";path=" + iPath;
    },

    getCookie: function (name) {
        let arr = document.cookie.split("; ");
        for (let i = 0; i < arr.length; i++) {
            let arr2 = arr[i].split("=");
            if (arr2[0] === name) {
                return decodeURI(arr2[1]);
            }
        }
        return "";
    },

    tree(oldArr, pid, id) {
        oldArr.forEach(element => {
            let parentId = element[pid];
            if (parentId !== 0) {
                oldArr.forEach(ele => {
                    if (ele[id] === parentId) {
                        if (!ele.son) {
                            ele.son = [];
                        }
                        ele.son.push(element);
                    }
                });
            } else {
                element['son'] = []
            }
        });
        oldArr = oldArr.filter(ele => ele[pid] === 0);
        return oldArr;
    },

    copyUrl(val) {
        var cInput = document.createElement("input");
        cInput.value = val;
        document.body.appendChild(cInput);
        cInput.select(); // 选取文本框内容
        document.execCommand("copy");
        document.body.removeChild(cInput);
        this.message('复制成功');
    },

    //字节转换成GB MB KB
    conver(limit) {
        var size = "";
        if (limit < 0.1 * 1024) { //如果小于0.1KB转化成B
            size = limit.toFixed(2) + "B";
        } else if (limit < 0.1 * 1024 * 1024) {//如果小于0.1MB转化成KB
            size = (limit / 1024).toFixed(2) + "KB";
        } else if (limit < 0.1 * 1024 * 1024 * 1024) { //如果小于0.1GB转化成MB
            size = (limit / (1024 * 1024)).toFixed(2) + "MB";
        } else { //其他转化成GB
            size = (limit / (1024 * 1024 * 1024)).toFixed(2) + "GB";
        }
        var sizestr = size + "";
        var len = sizestr.indexOf("\.");
        var dec = sizestr.substr(len + 1, 2);
        if (dec == "00") {//当小数点后为00时 去掉小数部分
            return sizestr.substring(0, len) + sizestr.substr(len + 3, 2);
        }
        return sizestr;
    },

};
